package cn.hftoday.util;

import java.util.*;

/**
 * Datetime parser
 * @author lane.cn@gmail.com
 */
public abstract class CalendarParser {
	
	/**
	 * invalid time
	 */
	public static final Calendar INVALID_TIME = CalendarParser.toCalendar("1900-01-01 00:00:00");
	
	/**
	 * min time
	 */
	public static final Calendar MIN_TIME = CalendarParser.toCalendar("1900-01-01 00:00:00");
	
	/**
	 * max time
	 */
	public static final Calendar MAX_TIME = CalendarParser.toCalendar("2999-12-31 23:59:59");
	
	/**
	 * convert datetime to string
	 * @param ca
	 * @param format
	 * @return
	 */
	public static String toString(Calendar ca, String format) {
		String y = Integer.toString(ca.get(Calendar.YEAR));
		String M = Integer.toString(ca.get(Calendar.MONTH) + 1);
		String d = Integer.toString(ca.get(Calendar.DATE));
		String h = Integer.toString(ca.get(Calendar.HOUR_OF_DAY));
		String m = Integer.toString(ca.get(Calendar.MINUTE));
		String s = Integer.toString(ca.get(Calendar.SECOND));
		
		String r = format;
		r = r.replaceAll("yyyy", StringParser.padLeft(y, "0000"));
		r = r.replaceAll("MM", StringParser.padLeft(M, "00"));
		r = r.replaceAll("dd", StringParser.padLeft(d, "00"));
		r = r.replaceAll("hh", StringParser.padLeft(h, "00"));
		r = r.replaceAll("mm", StringParser.padLeft(m, "00"));
		r = r.replaceAll("ss", StringParser.padLeft(s, "00"));
		
		return r;
	}

	/**
	 * convert datetime to string
	 * @param ca
	 * @return
	 */
	public static String toString(Calendar ca) {
	    return toString(ca, "yyyy-MM-dd hh:mm:ss");
	}
	
	/**
	 * convert string to datetime
	 * @param s
	 * @param format yyyy-MM-dd hh:mm:ss
	 * @return
	 */
	public static Calendar toCalendar(String s, String format) {
		Calendar ca = Calendar.getInstance();
		
		if (format.equals("yyyy-MM-dd hh:mm:ss")) {
			s = s.replaceAll("-", " ");
			s = s.replaceAll(":", " ");
			s = s.replaceAll("[.]", " ");
			String[] ary = s.split(" ");
			
			ca.set(Integer.parseInt(ary[0]), Integer.parseInt(ary[1]) - 1, Integer.parseInt(ary[2]), 
					Integer.parseInt(ary[3]), Integer.parseInt(ary[4]), Integer.parseInt(ary[5]));
		}
		else if (format.equals("yyyy-MM-dd")) {
			s = s.replaceAll("-", " ");
			s = s.replaceAll(":", " ");
			s = s.replaceAll(".", " ");
			String[] ary = s.split(" ");
			
			ca.set(Integer.parseInt(ary[0]), Integer.parseInt(ary[1]) - 1, Integer.parseInt(ary[2]), 
					0, 0, 0);
		}
		else if (format.equals("yyyyMMddhhmmss")) {
			int pos1 = format.indexOf('y');
			int pos2 = format.lastIndexOf('y');
			String y = s.substring(pos1, pos2 + 1);
			
			pos1 = format.indexOf('M');
			pos2 = format.lastIndexOf('M');
			String M = s.substring(pos1, pos2 + 1);
			
			pos1 = format.indexOf('d');
			pos2 = format.lastIndexOf('d');
			String d = s.substring(pos1, pos2 + 1);
			
			pos1 = format.indexOf('h');
			pos2 = format.lastIndexOf('h');
			String h = s.substring(pos1, pos2 + 1);

			pos1 = format.indexOf('m');
			pos2 = format.lastIndexOf('m');
			String m = s.substring(pos1, pos2 + 1);

			pos1 = format.indexOf('s');
			pos2 = format.lastIndexOf('s');
			String se = s.substring(pos1, pos2 + 1);
			
			ca.set(Integer.parseInt(y), Integer.parseInt(M) - 1, Integer.parseInt(d), 
					Integer.parseInt(h), Integer.parseInt(m), Integer.parseInt(se));
		}
		else if (format.equals("yyyyMMdd")) {
			int pos1 = format.indexOf('y');
			int pos2 = format.lastIndexOf('y');
			String y = s.substring(pos1, pos2 + 1);
			
			pos1 = format.indexOf('M');
			pos2 = format.lastIndexOf('M');
			String M = s.substring(pos1, pos2 + 1);
			
			pos1 = format.indexOf('d');
			pos2 = format.lastIndexOf('d');
			String d = s.substring(pos1, pos2 + 1);
			
			ca.set(Integer.parseInt(y), Integer.parseInt(M) - 1, Integer.parseInt(d), 
					0, 0, 0);
		}
		ca.set(Calendar.MILLISECOND, 0);
		return ca;
	}

	/**
	 * convert string to datetime
	 * @param s
	 * @return
	 */
	public static Calendar toCalendar(String s) {
	    return toCalendar(s, "yyyy-MM-dd hh:mm:ss");
	}
}
